<?

//Data loading
$title='ACCOUNT ACTIVITY';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. AMPEL JAYA';
$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();
$Result = GridList ("", "", "");
if ($Result) $pdf->GridTable($Result);
$pdf->Output();

function GridList ($Space, $Index, $ParentID) {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

            if ($Month<10) $Month = "0".$Month;
            if (($Month-1)<10) $LastMonth = "0".($Month-1);

            if ($Month<2) $LastMonth = "awal as lastmonth";
            else {$LastMonth = "aku".$LastMonth." as lastmonth";}

            if (GetParam("fromDate","")) {
                list($day,$month,$year) = explode("/",GetParam("fromDate",""));
                $fromDate = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
                $WHERE .= " AND tglbkt>='$fromDate'";
            }

            if (GetParam("toDate","")) {
                list($day,$month,$year) = explode("/",GetParam("toDate",""));
                $toDate   = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
                $WHERE .= " AND tglbkt<='$toDate'";
            }

            if (GetParam("fromCOA",""))
                $WHERE .= " AND ais_memorial.skada = '".GetParam("fromCOA","")."'";


            if (GetParam("office",""))
                $WHERE .= " AND ais_memorial.ktrasl='".GetParam("office","")."'";

            $SQL  = "
                SELECT ais_memorial.nobkt,ais_memorial.debnam,ais_memorial.tglbkt,ais_memorial.skada,ais_memorial.uraian,ais_memorial.dk,ais_memorial.rupiah,$LastMonth, DATE_FORMAT(tglbkt,'%d/%m/%y') as trdate
                FROM ais_memorial LEFT JOIN tblcoa ON ais_memorial.skada=tblcoa.skada
                WHERE 1
            ";

            //.
            if (strlen($WHERE)) $SQL .= $WHERE;
            $SQL .= " ORDER BY ais_memorial.SKADA,ais_memorial.tglbkt";
            //print $SQL;die();

            $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
            return $MyResult;
}

function GetSaldoAwal($Month,$year) {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            if ($Month<10) $Month = "0".$Month;
            if (($Month-1)<10) $LastMonth = "0".($Month-1);

            if ($Month<2) $LastMonth = "awal as lastmonth";
            else {$LastMonth = "aku".$LastMonth." as lastmonth";}

            $SQL = "SELECT $LastMonth FROM tblcoa WHERE ledger='$year'";
            //print $SQL;die();
            return $DBConnection->dbc->get_var($SQL);
}

function ReadCOA($no) {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            $SQL = "SELECT namacc FROM tblcoa WHERE skada='".$no."'";
            $MyResult = $DBConnection->dbc->get_var($SQL);
            return $MyResult;
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return $arraymonth[$month].", $year";
}


class PDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;

        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }

          $this->SetFont('arialn','',10);
        $this->Cell(180,5,$perush,0,1,'C',0);
        $this->Cell(180,5,$title,0,1,'C',0);
        $this->Cell(180,5,"PERIOD: ".GetParam("fromDate","")." TO ".GetParam("toDate",""),0,1,'C',0);
      $this->Ln();

      $this->Cell(100,5,"SKADA: ".GetParam("fromCOA","")." - ".ReadCOA(GetParam("fromCOA","")),0,0,'L',0);
        $this->Cell(100,5,$page." ".$this->PageNo(),0,1,'R',0);

        //Column widths
        $w=array(25,20,45,20,40,25,25);
        $this->SetFont('arialn','',8);
        $this->Cell($w[0],5,"TRANS. NO",1,0,'C',0);
        $this->Cell($w[1],5,"DATE",1,0,'C',0);
        $this->Cell($w[2],5,"DEBTUR",1,0,'C',0);
        $this->Cell($w[3],5,"SKADA",1,0,'C',0);
        $this->Cell($w[4],5,"DESCRIPTION",1,0,'C',0);
        $this->Cell($w[5],5,"DEBET",1,0,'C',0);
        $this->Cell($w[6],5,"CREDIT",1,0,'C',0);
        $this->Ln();
    }

    function GridTable($MyResult) {
        //Column widths
        $w=array(25,20,45,20,40,25,25);

        //Data


        foreach($MyResult as $row)
        {
            if ($row["dk"]=='D') $row["rupiah1"] = $row["rupiah"];
            else $row["rupiah2"] = $row["rupiah"];
            $totald = $totald + $row["rupiah1"];
            $totalc = $totalc + $row["rupiah2"];

            if (($skada<>$row["skada"]) && (strlen($skada)>0)) {
                $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SUB TOTAL:",1,0,'R',0);
                $this->Cell($w[5],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
                $this->Cell($w[6],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
                $this->Ln();
                $subtotald = 0;
                $subtotalc = 0;
                $this->AddPage();
            }

            //*********** SALDO AWAL *************
            if (($skada<>$row["skada"])) {
                $Saldo = $row["lastmonth"];
                if ($Saldo>0) $SaldoDebet = $Saldo;
                else $SaldoKredit = $Saldo;

                $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SALDO AWAL (".$row["skada"]."):",'BTLR',0,'R');
                $this->Cell($w[5],5,number_format($SaldoDebet,2),'LR',0,'R');
                $this->Cell($w[6],5,number_format($SaldoKredit,2),'LR',0,'R');
                $this->Ln();
            }
            //*********** SALDO AWAL *************

            $subtotald = $subtotald + $row["rupiah1"];
            $subtotalc = $subtotalc + $row["rupiah2"];

            $this->Cell($w[0],5,$row["nobkt"],'LR');
            $this->Cell($w[1],5,$row["trdate"],'LR',0,'C');
            $this->Cell($w[2],5,$row["debnam"],'LR',0,'L');
            $this->Cell($w[3],5,$row["skada"],'LR');
            $this->Cell($w[4],5,$row["uraian"],'LR');
            $this->Cell($w[5],5,number_format($row["rupiah1"],2),'LR',0,'R');
            $this->Cell($w[6],5,number_format($row["rupiah2"],2),'LR',0,'R');
            $this->Ln();
            $skada = $row["skada"];
        }
        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SUB TOTAL:",1,0,'R',0);
        $this->Cell($w[5],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
        $this->Cell($w[6],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
        $this->Ln();
/*
        $this->Cell(130,5,"TOTAL:",1,0,'R',0);
        $this->Cell(25,5,number_format($totald,2),1,0,'R',0);
        $this->Cell(25,5,number_format($totalc,2),1,0,'R',0);
        $this->Ln();
*/
        //Closure line
    }

    function Footer() {
        //Closure line
        $this->Cell(180,0,'','T');
    }

}


?>
